package com.example.community.service.impl;

import com.example.community.DTO.AddArticle;
import com.example.community.entity.article;
import com.example.community.service.articleService;
import com.example.community.utils.OssProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

@Service
public class articleImpl implements articleService {

    @Autowired
    private com.example.community.repository.articleMapper articleMapper;

    @Override
    public List<article> getAll() {
        return articleMapper.getAllArticle();
    }

    @Override
    public article getOneById(String id) {
        return articleMapper.getOneArticleById(id);
    }

    @Override
    public String addOne(AddArticle addArticleDTO) {
        String articleId = UUID.randomUUID().toString();
        article article = new article();
        article.setId(articleId);
        article.setAuthor(addArticleDTO.getAuthor());
        article.setTitle(addArticleDTO.getTitle());
        article.setContent(addArticleDTO.getContent());
        article.setCover_url(addArticleDTO.getCover_url());
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        article.setCreated_at(currentTime);
        articleMapper.addArticle(article);
        return articleId;
    }

    @Override
    public article updateOne(article article) {
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        article.setCreated_at(currentTime);
        articleMapper.updateArticle(article);
        return article;
    }

    @Override
    public int deleteOneArticle(String id) {
        articleMapper.deleteOneArticle(id);
        return 0;
    }


}
